package com.globallogic.socialnetwork.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.globallogic.socialnetwork.dao.UserDao;
import com.globallogic.socialnetwork.entity.Group;
import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.util.DaoException;

@WebService(endpointInterface = "com.globallogic.socialnetwork.service.SearchService")
public class SearchServiceImpl extends SpringBeanAutowiringSupport implements
		SearchService {
	@Autowired
	private UserDao userDao;
	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	@Override
	public List<Group> searchGroups(String userLogin, String condition)
			throws DaoException {
		log.warn("web service");
		User user = userDao.getUserByLogin(userLogin);
		List<Group> founded = userDao.searchGroups(condition);
		log.warn(founded);
		List<Group> userGroups = userDao.searchUserGroups(user);
		log.warn(userGroups);
		founded = ListUtils.removeAll(founded, userGroups);
		log.warn(founded);
		for (Group group : founded) {
			group.setUsers(null);
		}
		return founded;

	}

}
